package com.zby.big_event_zby.service.Imp;

import com.zby.big_event_zby.mapper.UserMapper;
import com.zby.big_event_zby.pojo.User;
import com.zby.big_event_zby.service.UserService;
import com.zby.big_event_zby.utils.JwtUtils;
import com.zby.big_event_zby.utils.Md5Util;
import com.zby.big_event_zby.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @version 1.0
 * @auther ZBY
 */
@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {


        return  userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        //密码加密
      String md5String = Md5Util.getMD5String(password);


        userMapper.register(username,md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());

        userMapper.update(user);
    }

    @Override
    public void updateAvatarUrl(String avatarUrl) {
        //获取id
        Map<String,Object> map = ThreadLocalUtils.get();
        Integer id =(Integer) map.get("id");
        userMapper.updateAvatarUrl(avatarUrl,id);
    }

    @Override
    public void updatePwd(String password) {
      Map<String,Object> map = ThreadLocalUtils.get();
        Integer id = (Integer)map.get("id");
        String md5String = Md5Util.getMD5String(password);
        userMapper.updatePwd(md5String,id);
    }
}
